package config

import (
	"server/share/protoc"

	"github.com/hwcer/yyds"
)

const PaymentCacheName = "PaymentCache"

func init() {
	yyds.Config.Register(&order{})
}

func GetPayment(order int32, platform int32) *protoc.Payment {
	c := yyds.Config.Process.Get(PaymentCacheName).(*paymentCache)
	k := c.Key(order, platform)
	return c.payment[k]
}

func GetPaymentWithCode(code string) *protoc.Payment {
	if code == "" {
		return nil
	}
	c := yyds.Config.Process.Get(PaymentCacheName).(*paymentCache)
	return c.paycode[code]
}

type paymentCache struct {
	payment map[uint64]*protoc.Payment
	paycode map[string]*protoc.Payment
}

func (paymentCache) Key(order int32, platform int32) uint64 {
	return uint64(order)<<32 + uint64(platform)
}

type order struct {
}

func (this *order) Handle(c *yyds.CS, i any) {
	data := i.(*output)
	d := &paymentCache{}
	d.payment = make(map[uint64]*protoc.Payment)
	d.paycode = make(map[string]*protoc.Payment)
	for _, v := range data.Payment {
		k := d.Key(v.Order, v.Platform)
		d.payment[k] = v
		if v.Code != "" {
			d.paycode[v.Code] = v
		}
	}

	c.Process.Set(PaymentCacheName, d)
	return
}

func (this *order) Verify(c *yyds.CS, i any) (errs []error) {
	return
}
